package com.atguigu.order.service.impl;

import com.atguigu.commonutils.vo.CourseWebVoOrder;
import com.atguigu.commonutils.vo.UcenterMemberVo;
import com.atguigu.order.client.ServiceEduClient;
import com.atguigu.order.client.ServiceUcenterClient;
import com.atguigu.order.entity.Order;
import com.atguigu.order.entity.vo.OrderQuery;
import com.atguigu.order.entity.vo.OrderVo;
import com.atguigu.order.mapper.OrderMapper;
import com.atguigu.order.service.OrderService;
import com.atguigu.order.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lgy
 * @since 2022-04-26
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    //远程调用serviceEdu
    @Autowired
    private ServiceEduClient serviceEduClient;

    //远程调用serviceUcenter
    @Autowired
    private ServiceUcenterClient serviceUcenterClient;

    //生成订单的方法
    @Override
    public String createOrders(String courseId, String memberId) {
        //根据用户id，获取用户信息
        UcenterMemberVo memberInfo = serviceUcenterClient.getMemberInfoById(memberId);

        //根据课程id，获取课程信息
        CourseWebVoOrder courseInfo = serviceEduClient.getCourseInfoById(courseId);

        Order tOrder = new Order();
        tOrder.setMobile(memberInfo.getMobile());
        tOrder.setNickname(memberInfo.getNickname());
        tOrder.setMemberId(memberId);
        tOrder.setCourseCover(courseInfo.getCover());
        tOrder.setCourseId(courseId);
        tOrder.setCourseTitle(courseInfo.getTitle());
        tOrder.setTeacherName(courseInfo.getTeacherName());
        tOrder.setTotalFee(courseInfo.getPrice());
        tOrder.setStatus(0);//支付状态：（ 0：已支付，1：未支付 ）
        tOrder.setPayType(1);//支付类型： 1：微信 ， 2：支付宝
        tOrder.setOrderNo(OrderNoUtil.getOrderNo()); //订单号

        //保存订单
        baseMapper.insert(tOrder);

        //返回订单号
        return tOrder.getOrderNo();
    }

    @Override
    public List<OrderVo> getOrdersPage(OrderQuery orderQuery) {
        /*String cname = null;
        Integer status = null;
        String begin = null;
        String end = null;

        if (!StringUtils.isEmpty(orderQuery)){
            cname = orderQuery.getCname();
            status = orderQuery.getStatus();
            begin = orderQuery.getBegin();
            end = orderQuery.getEnd();
        }


        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接条件
        if (!StringUtils.isEmpty(cname)){//一级分类
            wrapper.eq("course_title",cname);
        }
        if (!StringUtils.isEmpty(status)){//二级分类
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin)){
            //构造条件
            wrapper.ge("gmt_create",begin);//ge：大于等于
        }
        if (!StringUtils.isEmpty(end)){
            //构造条件
            wrapper.le("gmt_modified",end);//le:小于等于
        }*/


        //wrapper.orderByDesc("gmt_create");

        List<OrderVo> ov = baseMapper.getOrderPage(orderQuery);
        return ov;

    }

    @Override
    public void dd() {
        baseMapper.dddd();
    }


}

